package com.zgxt.demo.controller;



import com.alibaba.fastjson.JSONObject;
import com.zgxt.demo.entity.Admin;
import com.zgxt.demo.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {




    @Resource
    private AdminService adminService;


    @RequestMapping("/admin_login")
    public String Admin_Login(@RequestParam String account, @RequestParam String password){
        JSONObject _output = new JSONObject();
        _output.put("code",200);
        _output.put("msg","登录成功");
        _output.put("data",adminService.adminLogin(account,password));
        return _output.toJSONString();
    }

    @PostMapping("/admin_name")
    public String Admin_Name(@RequestParam Integer id,@RequestParam String name){
        JSONObject _outPut = new JSONObject();
        Integer tid= adminService.updateAdmin(id,name);
        if (tid==1){
            _outPut.put("code",200);
            _outPut.put("msg","修改成功");
        }else {
            _outPut.put("code",500);
            _outPut.put("msg","修改失败");
        }
        return _outPut.toJSONString();
    }

    @PostMapping("/admin_password")
    public String AdminPassword(@RequestParam Integer id,@RequestParam String password,@RequestParam String confirm_password){
        JSONObject _outPut = new JSONObject();
        if (!password.equals(confirm_password)){
            _outPut.put("code",500);
            _outPut.put("msg","两次密码不一致");
            return _outPut.toJSONString();
        }
        Integer tid= adminService.updatePassword(id,password);
        if (tid==1){
            _outPut.put("code",200);
            _outPut.put("msg","修改成功");
        }else if (tid==2){
            _outPut.put("code",500);
            _outPut.put("msg","密码不能与原密码相同");
        }else {
            _outPut.put("code", 500);
            _outPut.put("msg", "修改失败");
        }
        return _outPut.toJSONString();
    }
}
